<template>
  <div class="per_box" v-loading="loading" element-loading-text="正在加载中，请耐心等待！" element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)">
    <div>
      <div style="margin-left:30%;margin-top:40%;font-size:2rem; float: left;">

        欢迎使用 {{ systemName }}!
      </div>
    </div>
  </div>
</template>
  
<script>
// import { ipcRenderer } from "electron";

// 登录认证
window.electron.ipcRenderer.on("authLogin", (e, data) => {
  if (data == "cation") {
    vm.getAuthStatus("aignin");
  }
});
// 注销认证
window.electron.ipcRenderer.on("cancelLogin", (e, data) => {
  if (data == "lation") {
    vm.getAuthStatus("cancellation");
  }
});
//isDataReady
window.electron.ipcRenderer.on("getregister", (e, data) => {
  if (data == "cation") {
    vm.dalist();
  }
});
var vm = null; //介质
export default {
  name: "indexs",
  data() {
    return {
      systemName: window.systemName,
      loginMode: window.loginMode,
      loading: true,
      timer: null,
    };
  },
  created() {
    this.dalist();
    this.browserRedirect();
    //=====关键
    vm = this; //=====关键
    // this.getAuthStatus();
  },
  methods: {
    dalist() {
      if (this.loginMode == "DEBUG") {
        this.$router.push("/login");
        window.electron.ipcRenderer.send("Success-status");
        return;
      }
      // if (trat != "tarys") {
      //   window.electron.ipcRenderer.send("listAll");
      // }
      this.$get("vgmp/config/getregisterState")
        .then((res) => {
          console.log(res, "222222222");
          window.electron.ipcRenderer.send("Success-status"); //成功状态
          let { data } = res.data;
          if (data.state == true) {
            this.$router.push("/login");
            // window.electron.ipcRenderer.send("windowLogin");
          } else if (data.state == false) {
            this.$router.push("/install");
            // window.electron.ipcRenderer.send("Certification-status");
          } else {
            this.$router.push("/login");
            // window.electron.ipcRenderer.send("windowLogin");
          }
        })
        .catch((err) => {
          console.log(err, "getregisterState异常捕获");
          this.$router.push("/install");
          // window.electron.ipcRenderer.send("Certification-status");
        });
    },
    //获取认证状态
    getAuthStatus(stain) {
      let that = this
      if (this.loginMode == "DEBUG") {
        //todo 提供一个全的托盘菜单，认证、注销、隐藏等。
        // window.electron.ipcRenderer.send("Certification-status"); //认证状态
        // window.electron.ipcRenderer.send("listAll");
        console.log(11222222222);
        window.electron.ipcRenderer.send("Success-status"); //成功状态
      } else {
        this.$get("/jrkz/auth/getauthstatus").then((res) => {
          console.log(res, "getauthstatus返回信息");
          // window.electron.ipcRenderer.send("Success-status"); //成功状态
          // 1 = 认证状态 ； 0 = 注销或未认证
          if (res.data.data.status == "1") {
            if (stain == "aignin") {
              this.$message({
                message: "已是认证状态！",
                type: "warning",
              });
            } else if (stain == "loginTry") {
              this.$message({
                message: "认证成功！",
                type: "success",
              });
            } else {
              this.calation();
            }
            // window.electron.ipcRenderer.send("Not-certified"); //注销或未认证
          } else if (res.data.status == "1") {
            if (stain == "aignin") {
              this.$message({
                message: "已是认证状态！",
                type: "warning",
              });
            } else if (stain == "loginTry") {
              this.$message({
                message: "认证成功！",
                type: "success",
              });
            } else {
              this.calation();
            }
            //todo 需进行接口返回数据确认
            // window.electron.ipcRenderer.send("Not-certified"); //注销或未认证
          } else {
            if (stain == "cancellation") {
              this.$message({
                message: "已是注销状态！",
                type: "warning",
              });
            } else if (stain == "loginTry") {
              this.$message({
                  message: "认证失败!",
                  type: "warning",
                })
            } else {
              this.loglist();
            }
            // window.electron.ipcRenderer.send("Certification-status"); //显示接入认证
          }
        });
      }
    },
    // 登录认证
    loglist() {
      let that = this
      const loading = this.$loading({
            lock: true,
            text: "正在认证，请稍等！",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
      // this.$router.push("/loginAuth");
      this.$post("/jrkz/auth/tmlogin").then((res) => {
        console.log(res, "tmlogin + 登录认证");
        if (res.data.errorcode == "0") {
          that.timer = setInterval(() => {
            that.getAuthStatus("loginTry")
            loading.close();
            clearInterval(that.timer)
          }, 5000);
        } else {
          this.$message({
            message: "认证失败，" + res.data.errmsg,
            type: "warning",
          });
          loading.close();
        }
        // this.getAuthStatus();
      })
      .catch((err) => {
        this.$message({
            message: "认证失败!",
            type: "warning",
          });
          loading.close();
      })
    },

    // 注销认证
    calation() {
      let that = this
      this.$postForJson("/jrkz/auth/authlogout").then((res) => {
        console.log(res, "authlogout + 注销认证");
        if (res.data.errorcode == "0") {
          that.$get("/jrkz/auth/getauthstatus").then((res) => {
            // 1 = 认证状态 ； 0 = 注销或未认证
            if (res.data.data.status == "0") {
              that.$message({
                message: "注销成功！",
                type: "success",
              });
            } else if (res.data.status == "0") {
              that.$message({
                message: "注销成功！",
                type: "success",
              });
            } else {
              that.$message({
                  message: "注销失败！",
                  type: "warning",
              });
            }
          });
        } else {
          this.$message({
            message: "注销失败，" + res.data.errmsg,
            type: "warning",
          });
        }
        // this.getAuthStatus();
      });
    },

    //获取操作系统类型
    browserRedirect() {
      var sUserAgent = navigator.userAgent;
      var isWin =
        navigator.platform == "Win32" || navigator.platform == "Windows";
      if (isWin) sessionStorage.setItem("operatingSystem", "Windows");
      var isMac =
        navigator.platform == "Mac68K" ||
        navigator.platform == "MacPPC" ||
        navigator.platform == "Macintosh" ||
        navigator.platform == "MacIntel";
      if (isMac) sessionStorage.setItem("operatingSystem", "Mac");
      var isUnix = navigator.platform == "X11" && !isWin && !isMac;
      if (isUnix) sessionStorage.setItem("operatingSystem", "Unix");
      var isLinux = String(navigator.platform).indexOf("Linux") > -1;
      if (isLinux) sessionStorage.setItem("operatingSystem", "Linux");
      // if (isWin) {
      //     var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
      //     if (isWin2K) sessionStorage.setItem('operatingSystem',"Win2000");
      //     var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
      //     if (isWinXP) sessionStorage.setItem('operatingSystem',"WinXP");
      //     var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
      //     if (isWin2003) sessionStorage.setItem('operatingSystem',"Win2003");
      //     var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
      //     if (isWinVista)sessionStorage.setItem('operatingSystem',"WinVista");
      //     var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
      //     if (isWin7) sessionStorage.setItem('operatingSystem',"Win7");
      //     var isWin10 = sUserAgent.indexOf("Windows NT 10") > -1 || sUserAgent.indexOf("Windows 10") > -1;
      //     if (isWin10) sessionStorage.setItem('operatingSystem',"Win10");
      // }
    },
  },
};
</script>
  
<style scoped>
.per_box {
  width: 100%;
  height: 100%;
}
</style>